package com.theeviljames.pure;

import java.util.Random;


public class Complex {

	private double r;
	private double i;
	private static final Random rand = new Random();
	
	public Complex(){
		r = 0.0;
		i = 0.0;
	}
	public static Complex getRandom(double limit, boolean signed){
		if(signed){
			return new Complex((2*limit*rand.nextDouble())-limit,(2*limit*rand.nextDouble())-limit);
		}
		else{
			return new Complex(limit*rand.nextDouble(),limit*rand.nextDouble());
		}
	}
	
	public double getI() {
		return i;
	}

	public void setI(double i) {
		this.i = i;
	}

	public double getR() {
		return r;
	}

	public void setR(double r) {
		this.r = r;
	}

	public Complex(double r, double i){
		this.r = r;
		this.i = i;
	}
	
	public Complex add(Complex other){
		double r2 = this.getR()+other.getR();
		double i2 = this.getI()+other.getI();
		Complex result = new Complex(r2, i2);
		return result;
	}
	
	public Complex sub(Complex other){
		double r2 = this.getR()-other.getR();
		double i2 = this.getI()-other.getI();
		Complex result = new Complex(r2, i2);
		return result;
	}
	
	public Complex mult(Complex other){
		double r2 = this.getR()*other.getR()-this.getI()*other.getI();
		double i2 = this.getR()*other.getI()+other.getR()*this.getI();
		Complex result = new Complex(r2,i2);
		return result;
	}
	
	public Complex div(Complex other){
		Complex conjugate = new Complex(other.getR(),-other.getI());
		Complex top = this.mult(conjugate);
		Complex bottom = other.mult(conjugate);
		double r2 = top.getR()/bottom.getR();
		double i2 = top.getI()/bottom.getR();
		Complex result = new Complex(r2,i2);
		return result;
	}
	
	public String toString(){
		return "(" + r + " + " + i + "i)";
	}
	
	public static void main(String[] args){
		Complex a = new Complex(5.0,-2.0);
		Complex b = new Complex(2.0,4.0);
		System.out.println(a);
		System.out.println(b);
		Complex c = a.mult(b);
		System.out.println(c);
		Complex d = new Complex(3.0, 2.0);
		Complex e = new Complex(4.0, -3.0);
		System.out.println(d);
		System.out.println(e);
		Complex f = d.div(e);
		System.out.println(f);
	}
	public Complex pow(int power){
		Complex result = (Complex)this.safeClone();
		for(int i = 0; i < power-1; i++){
			result = mult(result);
		}
		return result;
	}
	public Complex sig1(){
		//  1/1+e^-x
		double r2 = 1/(1+(Math.pow(Math.E,r)));
		double i2 = 1/(1+(Math.pow(Math.E,i)));
		return new Complex(r2,i2);
	}
	public Complex d1(){
		Complex g = this.sig1();
		double r2 = 1-g.getR();
		double i2 = 1-g.getI();
		Complex a = new Complex(r2,i2);
		return g.mult(a);
	}
	public Object clone(){
		Complex c = new Complex(r,i);
		return c;
	}
	public Complex safeClone(){
		try{
			return (Complex)clone();
		}
		catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
	
}
